package com.yozinga.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.yozinga.MerchantDetailActivity;
import com.yozinga.R;
import com.yozinga.adapter.CategaryAdapter;
import com.yozinga.entity.Category;
import com.yozinga.entity.Offers;
import com.yozinga.model.BaseModel.OnRequestFinishedCallback;
import com.yozinga.model.CategoryModel;
import com.yozinga.processor.BusinessTypeProcessor;
import com.yozinga.processor.CategoryProcessor;
import com.yozinga.service.Actions;
import com.yozinga.util.AppData;
import com.yozinga.util.SharedData;
import com.yozinga.util.ToolKit;

public class CategoryListFragment extends ListFragment implements OnRequestFinishedCallback,OnClickListener{

	CategoryModel categoryModel;
	String subId,categoryName,subCategoryName,allId,ScreenName,filterId;
	Bundle bundle ;
	ArrayList<Category> list;
	HashMap<String,String> mBusinessTypeHashMap;
	public static ArrayList<Offers> offerList;
	SharedData mSharedData;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		bundle = this.getArguments();
		categoryName = bundle.getString("categoryName");
		ScreenName = categoryName;
		if(categoryName.contains("Travel and Hotels")){
			categoryName = "TH";
		}
		allId = bundle.getString("id");
		Log.i("Today","Id is: "+allId);
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_category, container,false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mSharedData = new SharedData(getActivity());
		ToolKit.startProgress(getActivity(),R.string.loading);
		categoryModel = new CategoryModel(getActivity());
		filterId = allId;
		categoryModel.getCategaryItems(new ToolKit(getActivity()).categoryServiceCall(),allId,Actions.ACTION_GET_LISTMERCHANTS_BYLOCATION);
		categoryModel.addOnServiceRequestCompletedListener(this);
		((TextView)getView().findViewById(R.id.all)).setTextColor(getResources().getColor(R.color.laun_green));
		((TextView)getView().findViewById(R.id.all)).setOnClickListener(this);
		
		((TextView)getView().findViewById(R.id.sub1)).setText(categoryName+" -1");
		((TextView)getView().findViewById(R.id.sub1)).setOnClickListener(this);
		
		((TextView)getView().findViewById(R.id.sub2)).setText(categoryName+" -2");
		((TextView)getView().findViewById(R.id.sub2)).setOnClickListener(this);
		
		((TextView)getView().findViewById(R.id.sub3)).setText(categoryName+" -3");
		((TextView)getView().findViewById(R.id.sub3)).setOnClickListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		categoryModel.removeOnServiceRequestCompleteListener(this);
	}

	@Override
	public void onRequestFinished(int resultCode, Bundle payload) {
		ToolKit.stopprogess();
		if(payload.get("action").equals("com.yozinga.ListMerchantsByLocation")){
			categoryModel.removeOnServiceRequestCompleteListener(this);
			String response = payload.getString("ServiceResponse");
			Log.i("Today","Restaurant Respons is :"+payload.getString("ServiceResponse"));
			CategoryProcessor categoryProcessor = new CategoryProcessor(filterId);
			list = categoryProcessor.getList(response);
		}else if(payload.get("action").equals("com.yozinga.ListSubCategoryByBusinessType")){
			categoryModel.removeOnServiceRequestCompleteListener(this);
			String response = payload.getString("ServiceResponse");
			Log.i("Today","ListSubCategoryByBusinessType Respons is :"+payload.getString("ServiceResponse"));
			BusinessTypeProcessor businessTypeProcess = new BusinessTypeProcessor(mSharedData,"Categories");
			mBusinessTypeHashMap = businessTypeProcess.getBusinessTypes(response);
			Log.i("Today","mBusinessTypeHashMap size is: "+mBusinessTypeHashMap.size());
			categoryModel = new CategoryModel(getActivity());
			categoryModel.addOnServiceRequestCompletedListener(this);
            subId = mBusinessTypeHashMap.get(subCategoryName.toLowerCase());
			Log.i("Today","SUbid is: "+subId);
			filterId = subId;
			categoryModel.getCategaryItems(new ToolKit(getActivity()).categoryServiceCall(),subId,Actions.ACTION_GET_LISTMERCHANTS_BYLOCATION);
		}
		ArrayList<String> mLogoList = new ArrayList<String>();
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http://4.bp.blogspot.com/-8v_k_fOcfP8/UQIL4ufghBI/AAAAAAAAEDo/9ffRRTM9AnA/s400/android-robog-alone.png&imgrefurl=http://about-google-android.blogspot.com/2013/01/rumors-on-new-coming-expected-android.html&h=750&w=1160&sz=488&tbnid=RhMVr2gPztn0aM:&tbnh=89&tbnw=137&prev=/search%3Fq%3Dandroid%2Bimages%26tbm%3Disch%26tbo%3Du&zoom=1&q=android+images&usg=__zYdPt3VI4l996udAqLXqgsz7X2o=&docid=5FGZK-n-4XZ08M&sa=X&ei=9UjrUauXFM7HrQfHuIHIBA&sqi=2&ved=0CE4Q9QEwCw&dur=1510#imgdii=RhMVr2gPztn0aM%3A%3BZinz8J9QQm5J5M%3BRhMVr2gPztn0aM%3A");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2F4.bp.blogspot.com%2F-8v_k_fOcfP8%2FUQIL4ufghBI%2FAAAAAAAAEDo%2F9ffRRTM9AnA%2Fs400%2Fandroid-robog-alone.png&imgrefurl=http%3A%2F%2Fabout-google-android.blogspot.com%2F2013%2F01%2Frumors-on-new-coming-expected-android.html&docid=5FGZK-n-4XZ08M&tbnid=RhMVr2gPztn0aM%3A&w=1160&h=750&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAIQxiAwAA&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		CategaryAdapter ca = new CategaryAdapter(list,mLogoList,getActivity());
		setListAdapter(ca);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Category category = (Category) list.get(position);
		String merchantId = category.getMerchantId();
		Log.i("Today","MerchantID is: "+merchantId+"Offer size is:"+category.getOffers());
		Intent merchantIntent = new Intent(getActivity(),MerchantDetailActivity.class);
		
		//Merchant ID for Offer Details
		merchantIntent.putExtra("merchantName", category.getBusinessName());
		merchantIntent.putExtra("merchantId",merchantId);
		
		
		//MerchantInfo information
		merchantIntent.putExtra("merchantVideoLink",category.getVideoUrl());
		merchantIntent.putExtra("merchantFullAddress",category.getMerchantFullAddress());
		merchantIntent.putExtra("merchantPhoneNumber",category.getPhoneNumber());
		merchantIntent.putExtra("merchantInfo","");
		merchantIntent.putExtra("merchantFacebook",category.getFaceBookUrl());
		merchantIntent.putExtra("merchantTwitter",category.getTwitterUrl());
		merchantIntent.putExtra("merchantPaypol",category.getPaypalEmail());
		merchantIntent.putExtra("categoryId",filterId);
		merchantIntent.putExtra("merchantTagLine", category.getMerchantTagLine());
		
		//MerchantInfo Map
		merchantIntent.putExtra("latitude",category.getLatitude());
		merchantIntent.putExtra("longitude",category.getLongitude());
		//merchantIntent.putExtra("offersList", category.getOfferDetails());
		offerList = category.getOfferDetails();
		
		startActivity(merchantIntent);
	}
	@Override
	public void onClick(View v) {
		ToolKit.startProgress(getActivity(),R.string.loading);
		categoryModel = new CategoryModel(getActivity());
		((TextView)getView().findViewById(R.id.all)).setTextColor(getResources().getColor(R.color.white));
		((TextView)getView().findViewById(R.id.sub1)).setTextColor(getResources().getColor(R.color.white));
		((TextView)getView().findViewById(R.id.sub2)).setTextColor(getResources().getColor(R.color.white));
		((TextView)getView().findViewById(R.id.sub3)).setTextColor(getResources().getColor(R.color.white));
		if(v.getId() == R.id.all){
			Log.i("Today","All");
			((TextView)getView().findViewById(R.id.all)).setTextColor(getResources().getColor(R.color.laun_green));
			if(categoryName.contains("-")){
				categoryName =  categoryName.substring(0,categoryName.indexOf("-"));
			}
			filterId = allId;
			categoryModel.getCategaryItems(new ToolKit(getActivity()).categoryServiceCall(),allId,Actions.ACTION_GET_LISTMERCHANTS_BYLOCATION);
		}else if(v.getId() == R.id.sub1){
			Log.i("Today","sub1");
			((TextView)getView().findViewById(R.id.sub1)).setTextColor(getResources().getColor(R.color.laun_green));
			subCategoryName = categoryName+"-1";
			if(mBusinessTypeHashMap!=null){
				subId = mBusinessTypeHashMap.get(subCategoryName.toLowerCase());
				Log.i("Today","SUbid is: "+subId+"categoryName is"+categoryName);
				filterId = subId;
				categoryModel.getCategaryItems(new ToolKit(getActivity()).categoryServiceCall(),subId,Actions.ACTION_GET_LISTMERCHANTS_BYLOCATION);
			}else{
			
				categoryModel.getCategaryItems(AppData.SUBCATEGORY_LIST,allId,Actions.ACTION_GET_SUB_CATEGORY_BY_BUSINESSTYPE);
			}
			
		}else if(v.getId() == R.id.sub2){
			Log.i("Today","sub2");
			((TextView)getView().findViewById(R.id.sub2)).setTextColor(getResources().getColor(R.color.laun_green));
			subCategoryName = categoryName+"-2";
			if(mBusinessTypeHashMap!=null){
				subId = mBusinessTypeHashMap.get(subCategoryName.toLowerCase());
				filterId = subId;
				categoryModel.getCategaryItems(new ToolKit(getActivity()).categoryServiceCall(),subId,Actions.ACTION_GET_LISTMERCHANTS_BYLOCATION);
			}else{
				
				categoryModel.getCategaryItems(AppData.SUBCATEGORY_LIST,allId,Actions.ACTION_GET_SUB_CATEGORY_BY_BUSINESSTYPE);
			}
			
		}else if(v.getId() == R.id.sub3){
			Log.i("Today","sub3");
			((TextView)getView().findViewById(R.id.sub3)).setTextColor(getResources().getColor(R.color.laun_green));
			subCategoryName = categoryName+"-3";
			if(mBusinessTypeHashMap!=null){
				subId = mBusinessTypeHashMap.get(subCategoryName.toLowerCase());
				filterId = subId;
				categoryModel.getCategaryItems(new ToolKit(getActivity()).categoryServiceCall(),subId,Actions.ACTION_GET_LISTMERCHANTS_BYLOCATION);
			}else{
				
				categoryModel.getCategaryItems(AppData.SUBCATEGORY_LIST,allId,Actions.ACTION_GET_SUB_CATEGORY_BY_BUSINESSTYPE);
			}
		}
		categoryModel.addOnServiceRequestCompletedListener(this);
	}

}
